package org.aeon.colorfulplanebattle.flying.entity;

import org.aeon.colorfulplanebattle.constant.ColorE;
import org.aeon.colorfulplanebattle.constant.DirectionE;
import org.aeon.colorfulplanebattle.flying.Utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import static org.aeon.colorfulplanebattle.constant.GameBasisConstant.*;

/**
 * @author: MissingAeon
 * @date: 2024/1/9 13:19
 * @description:
 */
public class Plane extends Ship {
    private static final Properties prop; // 这里保存配置信息
    private static final List<Map<String, BufferedImage>> pictures = new ArrayList<>();// 飞机图片集

    // 加载飞机图片
    static {
        prop = Utils.getProp(PLANE);
        try {
            for (int i = 0; ; i++) {
                InputStream resource;
                HashMap<String, BufferedImage> map = new HashMap<>();
                for (ColorE colorE : ColorE.values()) {
                    resource = Ship.class.getClassLoader().getResourceAsStream("img/feiji/feiji" + i + "_" + colorE.name() + ".png");
                    if (resource == null) break;
                    map.put(colorE.name(), ImageIO.read(resource));
                }
                if (map.isEmpty()) break;
                pictures.add(map);
            }
        } catch (IOException ignored) {
        }
    }

    private boolean isChange;

    public Plane() {
        super(FRIENDLY_CAMP);

        Utils.stuffing(prop, this);

        this.picture = pictures.get(0).get(ColorE.BLUE.name());

        this.y = BATTLEFIELD_HEIGHT / 2 + 50;
        this.x = (BATTLEFIELD_WIDTH - this.width) / 2;
        this.hp = this.maxHp;
    }

    @Override
    public String toString() {
        return "Plane{" +
                "isShooting=" + isShooting +
                ", maxHp=" + maxHp +
                ", hp=" + hp +
                ", bulletSpeed=" + bulletSpeed +
                ", x=" + x +
                ", y=" + y +
                ", width=" + width +
                ", height=" + height +
                ", touchWidth=" + touchWidth +
                ", touchHeight=" + touchHeight +
                ", camp=" + camp +
                ", color=" + color +
                ", speed=" + speed +
                ", isDestroy=" + isDestroy +
                '}';
    }

    @Override
    public boolean move(DirectionE direction) {
        // 玩家无法超过边缘
        switch (direction) {
            case UP -> y = Math.max(0, y - speed);
            case DOWN -> y = Math.min(BATTLEFIELD_HEIGHT - height, y + speed);
            case LEFT -> x = Math.max(0, x - speed);
            case RIGHT -> x = Math.min(BATTLEFIELD_WIDTH - width, x + speed);
        }
        return true;
    }

    public boolean isChange() {
        return isChange;
    }

    public void setChange(boolean change) {
        isChange = change;
    }

    @Override
    public void change() {
        this.picture = pictures.get(0).get(this.getColor());
    }
}
